<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>

<body>
    <button id="reset">重新开始一局游戏</button><br />
    <span>请输入要猜的数字：</span><input type="number" name="" id="userGuess"><button id="guess">猜</button><br />
    <span>已经猜的次数：</span><span id="count">0</span><br />
    <span>结果：</span><span id="result"></span>

    <script src="j/jquery-3.7.1.min.js"></script>
    <script>
        let number = Math.floor(Math.random() * 100) + 1;//[0,1)
        console.log(number);
        
        let count = 0;
        $("#guess").click(function(){
            let userGuessNum = $("#userGuess").val();//获取用户猜的值
            
            count++;
            $("#count").text(count);
            if(userGuessNum > number){
                //用户猜大了
                $("#result").text("猜大了");
                $("#result").css("color","red");
            }else if(userGuessNum < number){
                //用户猜小了
                $("#result").text("猜小了");
                $("#result").css("color","red");
            }else{
                //用户猜对了
                $("#result").text("猜对了");
                $("#result").css("color","green");

            }
        });

        $("#reset").click(function(){
            number = Math.floor(Math.random() * 100) + 1;
            console.log(number);
            count = 0;
            $("#count").text(count);
            $("#result").text("");
            $("#userGuess").val("");
        });
    </script>
</body>

</html>